package com.hui.security.app.controller;

import com.hui.security.app.entity.App;
import com.hui.security.app.service.AppService;
import com.hui.security.app.vo.AppVO;
import com.hui.security.common.response.ResultMapper;
import com.hui.security.common.response.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <b><code>AppController</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2019/1/22 15:37.
 *
 * @author Hu weihui
 */
@RestController
@RequestMapping("/v1")
@Api(value = "应用API", tags = "app")
@Slf4j
public class AppController {

    @Autowired
    private AppService appService;

    /**
     * Create app result vo.
     *
     * @param appVO         the app vo
     * @param bindingResult the binding result
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "新增应用程序", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/apps")
    public ResultVO createApp(@RequestBody @Validated AppVO appVO, BindingResult bindingResult) {
        App app = new App();

        BeanUtils.copyProperties(appVO, app);

        return ResultMapper.ok(appService.add(app));
    }


    /**
     * Delete app result vo.
     *
     * @param appId the app id
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "新增应用程序", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @DeleteMapping("/apps/{id}")
    public ResultVO deleteApp(@PathVariable("id") String appId) {

        appService.delete(appId);

        return ResultMapper.ok();
    }

    /**
     * Update app result vo.
     *
     * @param appVO         the app vo
     * @param bindingResult the binding result
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "更新应用程序", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PutMapping("/apps")
    public ResultVO updateApp(@RequestBody @Validated AppVO appVO, BindingResult bindingResult) {

        App app = new App();

        BeanUtils.copyProperties(appVO, app);

        appService.update(app);

        return ResultMapper.ok();
    }

    /**
     * List app result vo.
     *
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "查询应用程序", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/apps/{id}")
    public ResultVO getAppInfo(@PathVariable("id") String appId) {
        return ResultMapper.ok(appService.get(appId));
    }


    /**
     * List app result vo.
     *
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "查询应用程序", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/apps")
    public ResultVO listApps(@RequestParam String appName) {
        return ResultMapper.ok(appService.listApp(appName));
    }


}
